package org.squabble.web.account;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.squabble.SquabbleConstants;
import org.squabble.domain.Account;
import org.squabble.domain.AccountProperty;
import org.squabble.service.AccountService;

@Controller
@RequestMapping("/account/details")
@SessionAttributes(value = { "account" })
public class AccountDetailsController extends AbstractPropertyController {

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	@Qualifier("validator")
	Validator validator;

	@Autowired
	AccountService accountService;
	
	@Autowired
	MessageSource messageSource;

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(@ModelAttribute("account") Account account,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		account = accountService.getAccount(account.getId());
		Map<String, AccountProperty> props = account.getAccountProperties();
		AccountDetailsCommand adc = new AccountDetailsCommand();
		adc.setBirthDate((Date) getPropertyValue(props, "birthDate"));
		adc.setGender((String) getPropertyValue(props, "gender", "U"));
		adc.setLocation((String) getPropertyValue(props, "location"));
		adc.setProfile((String) getPropertyValue(props, "profile"));
		adc.setUrl((String) getPropertyValue(props, "url"));
		adc.setAim((String) getPropertyValue(props, "aim"));
		adc.setGoogle((String) getPropertyValue(props, "google"));
		adc.setIcq((String) getPropertyValue(props, "icq"));
		adc.setMsn((String) getPropertyValue(props, "msn"));
		adc.setPs3((String) getPropertyValue(props, "ps3"));
		adc.setSteam((String) getPropertyValue(props, "steam"));
		adc.setTwitter((String) getPropertyValue(props, "twitter"));
		adc.setXbox((String) getPropertyValue(props, "xbox"));
		adc.setXfire((String) getPropertyValue(props, "xfire"));
		adc.setYahoo((String) getPropertyValue(props, "yahoo"));
		adc.setWii((String) getPropertyValue(props, "wii"));
		adc.setAge((Long) getPropertyValue(props, "age"));
		Map<String, String> genderMap = new HashMap<String, String>();
		genderMap.put("M", messageSource.getMessage("details.gender.male", null, request.getLocale()));
		genderMap.put("F", messageSource.getMessage("details.gender.female", null, request.getLocale()));
		genderMap.put("U", messageSource.getMessage("details.gender.undisclosed", null, request.getLocale()));
		modelMap.addAttribute("genderList", genderMap);
		modelMap.addAttribute("accountDetailsCommand", adc);
		return "account.details";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(@ModelAttribute("account") Account account,
			@ModelAttribute("accountDetailsCommand") AccountDetailsCommand adc,
			BindingResult result, SessionStatus status, ModelMap model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		validator.validate(adc, result);
		if (result.hasErrors() && !result.hasFieldErrors("birthDate")) {
			return "account.details";
		}
		account = accountService.updateDetails(account.getId(), adc);
		model.addAttribute("account", account);
		model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
				"details.processed");
		return "redirect:/account/details";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		CustomDateEditor cde = new CustomDateEditor(sdf, true);
		binder.registerCustomEditor(Date.class, cde);
	}

}
